package pswvclass.pswvandroid2.com.kwords.share;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import pswvclass.pswvandroid2.R;
import pswvclass.pswvandroid2.com.kwords.KwordSee;
import pswvclass.pswvandroid2.com.po.ShareKWord;
import pswvclass.pswvandroid2.com.util.DBHelper;
import pswvclass.pswvandroid2.com.util.HttpUtils;

/**
 * Created by Administrator on 2018/11/2.
 */

/**
 * 长按菜单响应函数
 */
public class ShareMyDisplay extends Fragment {
    private ListView listView;
    private String tag = "shareMyDisplay";
    private TextView pswv_id;
    private TextView pswv_kword;
    private TextView pswv_context;
    MyAdapter my;
    protected static final int ERROR = 1;
    protected static final int SUCCESS = 0;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        return inflater.inflate(R.layout.pswv_kword_display_list, null);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            String data = bundle.getString("data");
            try {
                List<ShareKWord> datas = new ArrayList<ShareKWord>();
                JSONArray ja = new JSONArray(data);
                for (int i = 0; i < ja.length(); i++) {
                    JSONObject temp = (JSONObject) ja.get(i);
                    ShareKWord wk = new ShareKWord();
                    wk.setState(temp.getInt("status"));
                    wk.setkWords(temp.getString("kWords"));
                    wk.setContent(temp.getString("content"));
                    datas.add(wk);
                }
                Collections.sort(datas, new Comparator<ShareKWord>() {
                    @Override
                    public int compare(ShareKWord h1, ShareKWord h2) {
                        return h2.getReleaseTime().compareTo(h1.getReleaseTime());
                    }
                });
                my = new MyAdapter(getContext(), datas);
                listView = (ListView) getView().findViewById(R.id.pswv_list);
                listView.setAdapter(my);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SUCCESS: {
                    break;
                }
                case ERROR: {
                    Toast.makeText(getContext(), "连接错误 ....", Toast.LENGTH_SHORT).show();
                    break;
                }
                default: {
                    break;
                }
            }
        }
    };

    class MyAdapter extends BaseAdapter implements AdapterView.OnItemClickListener {
        private LayoutInflater mInflater;
        private List<ShareKWord> datas;

        public MyAdapter(Context context, List<ShareKWord> contentList) {
            this.mInflater = LayoutInflater.from(context);
            this.datas = contentList;
        }

        @Override
        public int getCount() {
            return datas.size();
        }

        @Override
        public Object getItem(int position) {

            return datas.get(position);
        }

        @Override
        public long getItemId(int position) {

            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = View.inflate(getContext(), R.layout.pswv_share_kword_my_list_see, null);
            pswv_id = (TextView) view.findViewById(R.id.pswv_id);
            TextView pswv_state = (TextView) view.findViewById(R.id.pswv_state);
            pswv_kword = (TextView) view.findViewById(R.id.pswv_kword);
            pswv_context = (TextView) view.findViewById(R.id.pswv_context);
            ShareKWord kw = datas.get(position);
            pswv_id.setText(String.valueOf(datas.get(position).getShareKWordId()));
            if (datas.get(position).getState() == 0) {
                pswv_state.setText("待审核");
            } else if (datas.get(position).getState() == 1) {
                pswv_state.setText("通过");
            } else if (datas.get(position).getState() == 2) {
                pswv_state.setText("驳回");
            }
            pswv_kword.setText("关键字：" + datas.get(position).getkWords());
            pswv_context.setText("内容：" + datas.get(position).getContent());
            ((ListView) parent).setOnItemClickListener(this);
            return view;
        }

        private void showDialog() {

        }

        @Override
        public void onItemClick(AdapterView<?> adapterView, final View view, final int i, long l) {
            PopupMenu popup = new PopupMenu(getContext(), view);
            MenuInflater inflater = popup.getMenuInflater();
            inflater.inflate(R.menu.share_kword_meun, popup.getMenu());
            popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    //当点击时弹出被点击象标题
                    switch (item.getItemId()) {
                        case R.id.kword_see: {
                            final ShareKWord kw1 = datas.get(i);
                            KwordSee ks = new KwordSee();
                            Bundle bundle = new Bundle();
                            bundle.putString("kWords", kw1.getkWords());
                            bundle.putString("content", kw1.getContent());
                            ks.setArguments(bundle);
                            getFragmentManager()
                                    .beginTransaction()
                                    .addToBackStack(null)
                                    .replace(R.id.realtabcontent, ks).commit();
                            break;
                        }
                        default: {
                            break;
                        }
                    }
                    return true;
                }
            });
            popup.show();
        }

    }


}
